package com.xxx.sqlJdbc;

import java.sql.*;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;

/**
 * @version v1.0
 * @Description
 * @Author admin
 * @date 2021/7/29  9:52
 * @Copyright
 */
public class JDbcDome {
    public static void main(String[] args) {

        ArrayList<Student> list = query();
        list.stream().forEach(System.out::println);
        LocalDateTime now = LocalDateTime.now();
        // Student student = new Student(100, "sss", 36, 178.6, "nan", now, 2);
        // insert(student);

    }

    private static void insert(Student student) {
        String sql = "insert into stu(name,age,height,sex,brithday,cla_id) values (?,?,?,?,?,?)";
        try (
                Connection connection = getConnection();
                PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ) {


        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection("jdbc:mysql://localhost:3306/jd13", "root", "123456");
    }

    private static ArrayList<Student> query() {
        ArrayList<Student> list = new ArrayList<>();
        try {
            Connection connection = getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement("select  * from stu");
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                Student student = new Student();
                student.setId(resultSet.getInt("id"));
                student.setName(resultSet.getString("name"));
                student.setAge(resultSet.getInt("age"));
                student.setHeight(resultSet.getDouble("height"));
                Timestamp birhtday = resultSet.getTimestamp("birhtday");
                Instant instant = birhtday.toInstant();
                String localDateTime = String.valueOf(LocalDateTime.ofInstant(instant, ZoneId.systemDefault()));
                student.setBirthday(localDateTime);
                student.setClaId(resultSet.getInt("cla_id"));
                list.add(student);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
}
